package main

// 14. 最长公共前缀 https://leetcode.cn/problems/longest-common-prefix/
func longestCommonPrefix(strs []string) string {
	ans := []byte(strs[0])
	for i := 1; i < len(strs); i++ {
		ans = longestCommonPrefixOfTwo(ans, []byte(strs[i]))
	}
	return string(ans)
}

func longestCommonPrefixOfTwo(s1, s2 []byte) []byte {
	n := len(s1)
	if len(s2) < n {
		n = len(s2)
	}
	ans := []byte{}
	for i := 0; i < n; i++ {
		if s1[i] != s2[i] {
			break
		}
		ans = append(ans, s1[i])
	}
	return ans
}

func min2[T int | string | float64 | float32](v1, v2 T) T {
	if v1 > v2 {
		return v2
	}
	return v1
}
